/*** Eclipse Class Decompiler plugin, copyright (c) 2016 Chen Chao (cnfree2000@hotmail.com) ***/
package shop.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import shop.dao.BaseDao;
import shop.entity.Orders;

public class OrdersDao extends BaseDao {
	private Connection conn = null;
	private PreparedStatement pstmt = null;
	private ResultSet rs = null;

	public int insertOrders(Orders orders) {
		Date day = new Date();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String sql = "insert into Orders values(?,?,?,?,?," + orders.getOrder_sum() + ",?)";
		String[] param = new String[] { orders.getOrder_id(), orders.getOrder_payment(), orders.getOrder_email(),
				orders.getOrder_user(), df.format(day), orders.getOrder_userId() };
		return this.executeSQL(sql, param);
	}

	public int insertOrderDetails(String order_number, String p_id, String p_price, String p_number) {
		String sql = "insert OrderDetails values (\'" + order_number + "\',\'" + p_id + "\',\'" + p_price + "\',\'"
				+ p_number + "\')";
		return this.executeSQL(sql, (String[]) null);
	}

	public List<Orders> fineOrder() {
		ArrayList list = new ArrayList();
		String sql = null;
		sql = "SELECT  * from OrderCollection";

		try {
			this.conn = this.getConn();
			this.pstmt = this.conn.prepareStatement(sql);
			this.rs = this.pstmt.executeQuery();

			while (this.rs.next()) {
				Orders e = new Orders();
				e.setOrder_id(this.rs.getString("order_id"));
				e.setOrder_email(this.rs.getString("order_email"));
				e.setOrder_payment(this.rs.getString("order_payment"));
				e.setOrder_sum(this.rs.getFloat("order_sum"));
				e.setOrder_time(this.rs.getString("order_time"));
				e.setOrder_user(this.rs.getString("order_user"));
				e.setP_id(this.rs.getString("p_id"));
				e.setP_number(this.rs.getString("p_number"));
				e.setP_price(this.rs.getInt("p_price"));
				e.setP_name(this.rs.getString("p_name"));
				list.add(e);
			}
		} catch (Exception arg6) {
			arg6.printStackTrace();
		} finally {
			this.closeAll(this.conn, this.pstmt, this.rs);
		}

		return list;
	}

	public int deleteORDER1(String order_id) {
		String sql = "DELETE FROM OrderDetails WHERE order_id =\'" + order_id + "\'";
		return this.executeSQL(sql, (String[]) null);
	}

	public int deleteORDER2(String order_id) {
		String sql = "DELETE FROM Orders WHERE order_id =\'" + order_id + "\'";
		return this.executeSQL(sql, (String[]) null);
	}

	public List<Orders> fineOrderBYUSER(String order_user) {
		ArrayList list = new ArrayList();
		String sql = null;
		sql = "SELECT  * from OrderCollection where order_user=\'" + order_user + "\' ORDER BY order_time DESC";

		try {
			this.conn = this.getConn();
			this.pstmt = this.conn.prepareStatement(sql);
			this.rs = this.pstmt.executeQuery();

			while (this.rs.next()) {
				Orders e = new Orders();
				e.setOrder_id(this.rs.getString("order_id"));
				e.setOrder_email(this.rs.getString("order_email"));
				e.setOrder_payment(this.rs.getString("order_payment"));
				e.setOrder_sum(this.rs.getFloat("order_sum"));
				e.setOrder_time(this.rs.getString("order_time"));
				e.setOrder_user(this.rs.getString("order_user"));
				e.setP_id(this.rs.getString("p_id"));
				e.setP_number(this.rs.getString("p_number"));
				e.setP_price(this.rs.getInt("p_price"));
				e.setP_name(this.rs.getString("p_name"));
				e.setOrder_userId(this.rs.getString("order_userId"));
				list.add(e);
			}
		} catch (Exception arg7) {
			arg7.printStackTrace();
		} finally {
			this.closeAll(this.conn, this.pstmt, this.rs);
		}

		return list;
	}

	public List<Orders> fineOrderBYuserID(String userID) {
		ArrayList list = new ArrayList();
		String sql = null;
		sql = "SELECT  * from OrderCollection where order_userId=\'" + userID + "\' ORDER BY order_time DESC";

		try {
			this.conn = this.getConn();
			this.pstmt = this.conn.prepareStatement(sql);
			this.rs = this.pstmt.executeQuery();

			while (this.rs.next()) {
				Orders e = new Orders();
				e.setOrder_id(this.rs.getString("order_id"));
				e.setOrder_email(this.rs.getString("order_email"));
				e.setOrder_payment(this.rs.getString("order_payment"));
				e.setOrder_sum(this.rs.getFloat("order_sum"));
				e.setOrder_time(this.rs.getString("order_time"));
				e.setOrder_user(this.rs.getString("order_user"));
				e.setP_id(this.rs.getString("p_id"));
				e.setP_number(this.rs.getString("p_number"));
				e.setP_price(this.rs.getInt("p_price"));
				e.setP_name(this.rs.getString("p_name"));
				e.setOrder_userId(this.rs.getString("order_userId"));
				list.add(e);
			}
		} catch (Exception arg7) {
			arg7.printStackTrace();
		} finally {
			this.closeAll(this.conn, this.pstmt, this.rs);
		}

		return list;
	}
}